package com.loong.shop.handler;

import com.loong.common.domain.BaseQuery;
import com.loong.common.domain.Pager;
import com.loong.common.enumerate.ResultCode;
import com.loong.common.exception.GlobalException;
import com.loong.common.executor.BatchExecutor;
import com.loong.common.utils.TransformUtils;
import com.loong.shop.business.GoodsSpecificationNameBusiness;
import com.loong.shop.common.backend.param.GoodsSizeNameParam;
import com.loong.shop.common.backend.param.GoodsSizeNameQueryParam;
import com.loong.shop.common.backend.result.GoodsSizeNameResult;
import com.loong.shop.common.backend.result.GoodsSizeValueResult;
import com.loong.shop.common.forehead.param.GoodsSizeNameQueryForeheadParam;
import com.loong.shop.common.forehead.result.GoodsSizeNameForeheadResult;
import com.loong.shop.model.GoodsSpecificationName;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Description: 处理商品规格名称业务的handler
 * @Author: 刘群
 * @Date: 2020/10/27 8:56
 */
@Component
public class GoodsSpecificationNameHandler {

    @Resource
    private GoodsSpecificationNameBusiness nameBusiness;

    @Resource
    private GoodsSpecificationValueHandler valueHandler;

    @Resource
    private BatchExecutor batchExecutor;

    /**
     * 后台新增商品规格名称
     * @param param 商品规格名称封装的参数
     */
    public void saveGoodsSpecificationName(GoodsSizeNameParam param){
        //先判断是否存在重复的规格名称，若存在则不能新增，若不存在则可以新增
        GoodsSizeNameQueryParam queryParam = GoodsSizeNameQueryParam.builder()
                .name(param.getName())
                .shopId(param.getShopId())
                .build();
        List<GoodsSpecificationName> nameList = nameBusiness.findDuplicationNamesByParam(queryParam);
        if (CollectionUtils.isEmpty(nameList)){
            //不存在则新增
            param.setCreateTime(new Date());
            nameBusiness.save(TransformUtils.transform(param, GoodsSpecificationName.class));
        }else {
            throw new GlobalException(ResultCode.PARAM_ERROR," specification name already exists");
        }
    }

    /**
     * 后台更新商品规格名称
     * @param param 商品规格名称封装的参数
     */
    public void updateGoodsSpecificationName(GoodsSizeNameParam param){
        //直接更新商品规格
        param.setLastUpdateTime(new Date());
        nameBusiness.updateByPrimaryKeySelective(TransformUtils.transform(param, GoodsSpecificationName.class));
    }

    /**
     * 后台批量删除商品规格名称
     * @param idList 商品规格名称id集合
     */
    public void deleteGoodsSpecificationName(List<Long> idList){
        batchExecutor.batchExecute(idList,id->id.toString(),(item)->{
            List<GoodsSizeValueResult> valueResultList = valueHandler.findGoodsSizeValueById(item);
            if (CollectionUtils.isEmpty(valueResultList)){
                nameBusiness.deleteById(item);
            }else {
                throw new GlobalException("specification name is used");
            }
        });
    }

    /**
     * 后台分页查询商品规格名称
     * @param query 查询参数
     * @return 分页结果
     */
    public Pager<GoodsSizeNameResult> pageQueryGoodsSpecificationName(BaseQuery query){
        Pager<GoodsSpecificationName> pager = nameBusiness.pageQuery(query, () -> nameBusiness.findAll());
        return TransformUtils.transformPage(pager, GoodsSizeNameResult.class);
    }

    /**
     * 前台查询商品规格名称
     * @param param 查询参数：店铺id、规格名称
     * @return 商品规格名称集合
     */
    public List<GoodsSizeNameForeheadResult> findSizeNameToForehead(GoodsSizeNameQueryForeheadParam param){
        return TransformUtils.transformList(nameBusiness.findSpecificationNameByParam(param),GoodsSizeNameForeheadResult.class);
    }

}
